#region "GlobalWebConfigurationCacheDependency"

/*
 * ####     Developer Name : Wang Yucai
 * 
 * ####     Development Tool : Microsoft VisualStudio 2010 Ultimate Edition
 * 
 * ####     Create Time : 2014-07-15 16:45:14
 * 
 * ####     Namespace : Masterduner.SourceStudio.Practices.DevelopmentLibrary.Web.Configuration
 * 
 * ####     Type Name : GlobalWebConfigurationCacheDependency
 * 
 * ####     Full Name : Masterduner.SourceStudio.Practices.DevelopmentLibrary.Web.Configuration.GlobalWebConfigurationCacheDependency
 * 
 * ####     Machine Name : GLCHQWYCWINW7
 * 
 * ####     UserName : GUOCOLAND/wangyucai
 * 
 * ####     CLR Version : 4.0.30319.18444
 * 
 * ####     Target Framework Version : 4.0
 */

#endregion

using System;
using System.IO;
using System.Web.Caching;
using Masterduner.SourceStudio.Practices.DevelopmentLibrary.Web.Runtime;
using System.Text;

namespace Masterduner.SourceStudio.Practices.DevelopmentLibrary.Web.Configuration
{
    /// <summary>
    /// <para>Masterduner.SourceStudio.Practices.DevelopmentLibrary.Web.Configuration.GlobalWebConfigurationCacheDependency</para>
    /// <para>
    /// Web应用配置全局缓存依赖。
    /// </para>
    /// </summary>
    /// <remarks>
    /// <para>Target Framework Version : 4.0</para>
    /// </remarks>
    public static class GlobalWebConfigurationCacheDependency
    {
        /// <summary>
        /// 同步锁对象实例。
        /// </summary>
        private static readonly object SynchronousLockedObject = new object();
        /// <summary>
        /// 全局缓存文件。
        /// </summary>
        public static readonly FileInfo GlobalCacheDependency = new FileInfo(Path.Combine(AspContextVariables.Context.ApplicationPhysicalDirectory, "aspnet.devlibs.globalcachedepency.wyc"));

        #region TryUpdate
        /// <summary>
        /// 尝试更新全局缓存依赖文件。
        /// </summary>
        /// <param name="reason"><see cref="CacheItemRemovedReason"/>中的一个值。</param>
        static private void TryUpdate(CacheItemRemovedReason reason)
        {
            lock (SynchronousLockedObject)
            {
                string infoText = string.Format("{0} : {1}", reason, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                using (FileStream stream = new FileStream(GlobalCacheDependency.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    try
                    {
                        byte[] buffer = infoText.GetBytes(UTF8Encoding.UTF8);
                        stream.Write(buffer, 0, buffer.Length);
                    }
                    finally
                    {
                        stream.Close();
                    }
                }
                try
                {
                    GlobalCacheDependency.Attributes = FileAttributes.Hidden;
                }
                catch{}
            }
        }
        #endregion

        #region CacheRemoveCallback
        /// <summary>
        /// 当缓存数据被移除时的会掉方法。
        /// </summary>
        /// <param name="key">移除的缓存键。</param>
        /// <param name="data">移除的缓存数据。</param>
        /// <param name="reason"><see cref="CacheItemRemovedReason"/>中的一个值。</param>
        internal static void CacheRemoveCallback(string key, object data, CacheItemRemovedReason reason)
        {
            TryUpdate(reason);
        }
        #endregion
    }
}

#region README

/*
 * ####     ©2014 Wang Yucai
 * 
 * ####     Contact me
 *                  
 *                  E-mail : Masterduner@Yeah.net
 *                  Mobile : 13621384677
 *                  QQ : 180261899
 */

#endregion